package com.sl.au.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Role;
import com.sl.au.entity.User;
import com.sl.au.repository.AccountRepository;
import com.sl.au.service.AccountService;
import com.sl.au.service.DeleteService;
import com.sl.au.service.RoleService;
import com.sl.au.service.UserService;
import com.sl.utils.ExdeUtil;

@RestController
@RequestMapping(value = "/ui/deleteExde") 
public class DeleteController extends BaseController {

	@Autowired
	DeleteService deleteService;
	
	@RequestMapping(value="one/{user}/{table}/{id}",method = RequestMethod.GET)
	public Map<String, Object> deleteOne(@PathVariable String user,@PathVariable String table,@PathVariable String id) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(user.equals("exde0410")) {
			String classPrefix = "com.sl.au.entity.";
			Class clazz;
			try {
				clazz = Class.forName(classPrefix+table);
				boolean del = deleteService.deleteInfoAllForClass(this.datasourceName, clazz, id);
				if(del) {
					map.put("msg", "删除成功！");
				}else {
					map.put("msg", "删除失败！");
				}
			} catch (ClassNotFoundException e) {
				map.put("msg", "没找到所对应的表！");
			}
		}else {
			map.put("msg", "权限不足！");
		}
		return map;
	}

	@RequestMapping(value="list/{user}/{table}/{begin}/{end}",method = RequestMethod.GET)
	public Map<String, Object> deleteList(@PathVariable String user,@PathVariable String table,@PathVariable String begin,@PathVariable String end) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(user.equals("exde0410")) {
			String classPrefix = "com.sl.au.entity.";
			Class clazz;
			String pattern = "yyyy-MM-dd";
			Date beginDate = ExdeUtil.getDateBegin(ExdeUtil.str2Date(begin, pattern));
			Date endDate = ExdeUtil.getDateEnd(ExdeUtil.str2Date(end, pattern));
			try {
				clazz = Class.forName(classPrefix+table);
				map = deleteService.deleteListAllForClass(this.datasourceName, clazz, beginDate,endDate);
			} catch (ClassNotFoundException e) {
				map.put("msg", "没找到所对应的表！");
			}
		}else {
			map.put("msg", "权限不足！");
		}
		return map;
	}
}
